﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Task.Entity;
using PowerCreator.LMS.Web.Task;
using System.Collections.Generic;
using PowerCreator.LMS.Common.Utils;

public partial class sysadmin_TaskManager_AddTarget : System.Web.UI.Page
{
    EvaluateManag objEvaluateManag = new EvaluateManag();
    TargetManag objTargetManag = new TargetManag();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            int EvateID=0, TargetFatherID=0;
            if (Request.Params["EvateID"] != null && Request.Params["EvateID"].Trim() != "")
            {
                EvateID = int.Parse(Request.Params["EvateID"].ToString());
                ViewState["EvateID"] = EvateID;
            }

            if (Request.Params["TargetFatherID"] != null && Request.Params["TargetFatherID"].Trim() != "")
            {
                TargetFatherID = int.Parse(Request.Params["TargetFatherID"].ToString());
                ViewState["TargetFatherID"] = TargetFatherID;
            }

            PageLoad(EvateID, TargetFatherID);
            
        }
    }
    private void PageLoad(int EvateID, int TargetFatherID)
    {
     
        Evaluate objEvaluate = objEvaluateManag.Load(EvateID);
        lblTargetEvate.Text = objEvaluate.EvateName;
        string search = " TargetType=0 and FKTargetEvateID=" + EvateID.ToString() + "";
        DataTable dtTarget = objTargetManag.Query(search);
        List<Target> list = objTargetManag.GetTargetTree(dtTarget);

        dpdlstTargetFatherName.DataTextField = "TargetName";
        dpdlstTargetFatherName.DataValueField = "TargetID";
        dpdlstTargetFatherName.DataSource = list;
        dpdlstTargetFatherName.DataBind();

        if (TargetFatherID != 0)
        {
            for (int i = 0; i < dpdlstTargetFatherName.Items.Count; i++)
            {
                if (dpdlstTargetFatherName.Items[i].Value == TargetFatherID.ToString())
                {
                    dpdlstTargetFatherName.Items[i].Selected = true;
                    break;
                }
            }
        }

    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string strErr = string.Empty;
        if (!PageValidate.IsNumber(txtTargetScore.Text))
        {
            strErr += "分数请输入数字！</br>";
           
        }
        if (!PageValidate.IsDecimal(txtTargetScale.Text))
        {
            strErr += "权重请输入浮点数！</br>";
        }
        if (strErr != "")
        {
            lbl_msg_Error.Text = strErr;
            return;
        }

        int FKTargetEvateID = int.Parse(ViewState["EvateID"].ToString());
		string TargetName=this.txtTargetName.Text;
        int TargetFatherID = int.Parse(dpdlstTargetFatherName.SelectedValue);
		int TargetScore=int.Parse(this.txtTargetScore.Text);
		string TargetScale=this.txtTargetScale.Text;
        int TargetType = 1;
        if (rdbtnItem.Checked)
        {
            TargetType = 0;
        }
		
		string TargetRemark=this.txtTargetRemark.Text;
        DateTime TargetCreateTime = DateTime.Now;
        int FKTargetCreateID = 10000;
        Target model = new Target();
  
        model.TargetFatherID = TargetFatherID;
        model.FKTargetEvateID = FKTargetEvateID;
        model.TargetType = TargetType;
        model.TargetCreateTime = TargetCreateTime;
        model.FKTargetCreateID = FKTargetCreateID;
  
		model.TargetName=TargetName;
		
		model.TargetScore=TargetScore;
		model.TargetScale=TargetScale;
		
		model.TargetRemark=TargetRemark;

        bool result;
        result = objTargetManag.AddNew(model);
        if (rdbtnTarget.Checked)
        {
            objTargetManag.UpdateTargetScore(FKTargetEvateID);
        }
        if (result)
        {
            Script.RedirectURL("ManagerTarget.aspx?EvateID=" + ViewState["EvateID"].ToString());
        }
      
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Script.RedirectURL("ManagerTarget.aspx?EvateID=" + ViewState["EvateID"].ToString());
    }
    protected void rdbtnTarget_CheckedChanged(object sender, EventArgs e)
    {
        txtTargetScore.Enabled = true;
        txtTargetScale.Enabled = true;
    }
    protected void rdbtnItem_CheckedChanged(object sender, EventArgs e)
    {
        txtTargetScore.Enabled = false;
        txtTargetScale.Enabled = false;
    }
}
